﻿using Furion.CMS.Core.Handlers;
using Furion.EventBus;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Furion.CMS.Core
{
    /// <summary>
    /// 数据审计内容
    /// </summary>
    public class DbAuditContent
    {
        public DbAuditContent()
        {
        }

        public DbAuditContent(string column, string columnName, object oldValue, object newValue)
            : this()
        {
            Column = column;
            ColumnName = columnName;

            // 常规值转换处理
            if (oldValue is Enum oldEnumValue) OldValue = oldEnumValue.GetDescription();
            else if (bool.TryParse(oldValue?.ToString(), out var flag)) OldValue = flag ? "是" : "否";
            else OldValue = oldValue?.ToString();

            if (newValue is Enum newEnumValue) NewValue = newEnumValue.GetDescription();
            else if (bool.TryParse(newValue?.ToString(), out var flag)) NewValue = flag ? "是" : "否";
            else NewValue = newValue?.ToString();
        }

        public DbAuditContent(string column, string columnName, object oldValue, object newValue, Type transitionHandle)
            : this(column, columnName, oldValue, newValue)
        {
            TransitionHandle = transitionHandle;
        }

        /// <summary>
        /// 列
        /// </summary>
        public string Column { get; set; }
        /// <summary>
        /// 列名称
        /// </summary>
        public string ColumnName { get; set; }
        /// <summary>
        /// 旧值
        /// </summary>
        public string OldValue { get; set; }
        /// <summary>
        /// 新值
        /// </summary>
        public string NewValue { get; set; }
        /// <summary>
        /// 转换处理器
        /// </summary>
        [JsonIgnore]
        public Type TransitionHandle { get; set; }
    }


    /// <summary>
    /// 数据库审计事件源
    /// </summary>
    public class DbAuditEventSource : IEventSource
    {
        public DbAuditEventSource()
        {
            EventId = EventIdConsts.DbAudit;
        }

        public DbAuditEventSource(string operate, string userId, string ipAddress, string appName, string table)
            : this()
        {
            Operate = operate;
            UserId = userId;
            AppName = appName;
            Table = table;
            IPAddress = ipAddress;
        }

        /// <summary>
        /// 操作状态
        /// </summary>
        public string Operate { get; }
        /// <summary>
        /// 操作用户id
        /// </summary>
        public string UserId { get; }
        /// <summary>
        /// 操作IP地址
        /// </summary>
        public string IPAddress { get; set; }
        /// <summary>
        /// 应用名称
        /// </summary>
        public string AppName { get; }
        /// <summary>
        /// 操作表
        /// </summary>
        public string Table { get; }
        /// <summary>
        /// 行Key
        /// </summary>
        public object RowKey { get; set; }
        /// <summary>
        /// 转换处理程序
        /// </summary>
        public Type TransitionHandle { get; set; }

        #region 基础属性
        public string EventId { get; }

        public object Payload { get; set; }

        public DateTime CreatedTime { get; } = DateTime.UtcNow;

        public CancellationToken CancellationToken { get; }
        #endregion
    }
}
